



(function(Global /*, context */) {

    Global.isAbilitiesShopOpened=false;
    var abilitiesShopELE=$("#AbilitiesShop");
    var selectedItem=null
    var topBtns=[];
    var isBuying=false;


    $("#CloseBtn").SetPanelEvent("onactivate", function () {
        ShowAbilitiesShop(false)
        CloseTopBtn()
    });

    $("#BasPrice").text=Global.GameConfig.basAbilityPrice;
    $("#AttrPrice").text=Global.GameConfig.attAbilityPrice;
    $("#UltPrice").text=Global.GameConfig.ultAbilityPrice;
    $("#PtPrice").text=Global.GameConfig.ptAbilityPrice;

    SetupBtnEvent($("#BasBtn"),"AbilitiesShop_AbilityType_Basic","DOTA_ABILITY_TYPE_BASIC");
    SetupBtnEvent($("#AttrBtn"),"AbilitiesShop_AbilityType_Attributes","DOTA_ABILITY_TYPE_ATTRIBUTES");
    SetupBtnEvent($("#UltBtn"),"AbilitiesShop_AbilityType_Ultimate","DOTA_ABILITY_TYPE_ULTIMATE");
    SetupBtnEvent($("#PtBtn"),"AbilitiesShop_AbilityType_Points","DOTA_ABILITY_TYPE_POINTS");

    Global.Panel.dotaHud.FindChildTraverse("lower_hud").FindChildTraverse("BuffContainer").FindChildTraverse("buffs").style.marginBottom="280px";
    Global.Panel.dotaHud.FindChildTraverse("lower_hud").FindChildTraverse("BuffContainer").FindChildTraverse("debuffs").style.marginBottom="280px";


    function SetupBtnEvent(panel,text,abilityType) {

        panel.SetPanelEvent("onmouseover", function () {
            // $.Msg(abilityImgELE)
            $.DispatchEvent('DOTAShowTextTooltip',panel,text );
        });
        panel.SetPanelEvent("onmouseout",  function (){
            $.DispatchEvent('DOTAHideTextTooltip');
        });
        panel.SetPanelEvent("onactivate",  function (){
            RefreshAbility(abilityType)
        })
    }




function OnSelectedItem(panel,data) {
    // $.Msg("OnSelectedItem",data)
        $.Msg(topBtns.length)
       if(selectedItem!==null){
           selectedItem.SetHasClass("AbilitiesShopItemSelected",false)
       }
        if(selectedItem==null||selectedItem!=panel){
                 Game.EmitSound("ui_select_md")
         }

       selectedItem=panel;
       selectedItem.SetHasClass("AbilitiesShopItemSelected",true)

       var localHeroIndex = Players.GetPlayerHeroEntityIndex( Players.GetLocalPlayer());

       var row=0;
       var col=6;
        switch (data.abilityType){
            case "DOTA_ABILITY_TYPE_BASIC":
                row=0;
                break;
            case "DOTA_ABILITY_TYPE_ATTRIBUTES":
                row=1;
                break;
            case "DOTA_ABILITY_TYPE_ULTIMATE":
                row=2;
                break;
        }

    if(topBtns.length!==Global.GameConfig.abilitiesSlotCount){
        InitTopBtn();
    }

       for(var i=0;i<col;i++){
            var curCol=i+row*col;
            if(topBtns[curCol]==null){
                CreateTopBtn(curCol)
            }

           if(topBtns[curCol]&&topBtns[curCol].style){
               topBtns[curCol].style.visibility='visible'
           }
           var abilityIndex = Entities.GetAbility( localHeroIndex ,curCol);
           var abilityName=Abilities.GetAbilityName( abilityIndex);
           if(abilityName==="ability_empty"||abilityName==="ability_empty2"){
               topBtns[curCol].FindChildTraverse("Icon").SetHasClass("IconChange",false)
                topBtns[curCol].FindChildTraverse("Icon").SetHasClass("IconLevel",true)
           }
           else {
               topBtns[curCol].FindChildTraverse("Icon").SetHasClass("IconChange",true)
               topBtns[curCol].FindChildTraverse("Icon").SetHasClass("IconLevel",false)
           }
       }

}

function InitTopBtn() {
    for(var i=0;i<Global.GameConfig.abilitiesSlotCount;i++){
            CreateTopBtn(i)
    }
}


    function CreateTopBtn(index) {
        if (topBtns[index] == null) {
            // $.Msg("CreateTopBtn", index)
            var abilityELE = Global.Panel.dotaHud.FindChildTraverse("Ability" + index);
            if (abilityELE !== null) {
                topBtns[index] = abilityELE.FindChildTraverse("ButtonAndLevel");
                if (topBtns[index] == null) {
                    return;
                }
                topBtns[index] = $.CreatePanel("Panel", abilityELE, "");
                topBtns[index].style.zIndex = "100";
                topBtns[index].BLoadLayout("file://{resources}/layout/custom_game/abilities_top_button.xml", false, false);
                topBtns[index].SetPanelEvent("onactivate", function () {
                    // $.Msg("selectedItem=",selectedItem.index)
                    BuyAbility(index)
                });
            }
        }
        if (topBtns[index] != null && topBtns[index].style) {
            topBtns[index].style.visibility = 'collapse'
        }
    }

function CloseTopBtn() {
    for(var i=0;i<Global.GameConfig.abilitiesSlotCount;i++){
        if(topBtns[i]){
            topBtns[i].style.visibility='collapse'
        }
    }
}

function ShowAbilitiesShop(isShow) {
    Global.isAbilitiesShopOpened = isShow;
    if(isShow==true){
        Game.EmitSound("Shop.PanelUp")
        abilitiesShopELE.style.position = '0px 0px 0px';
        abilitiesShopELE.style.transform = 'scale3d( 1,1,1)';
    }
    else {
        Game.EmitSound("Shop.PanelDown")
        abilitiesShopELE.style.position = '-400px -250px 0px';
        abilitiesShopELE.style.transform = 'scale3d( 0.001,0.001,0.001)';
    }
    // Global.Panel.topBarAbilitiesShop.SetHasClass("Selected",isShow)
}


function  RefreshAbilitiesShop(data){
    $.Msg("[RefreshAbilitiesShop]   ",data)
    if(selectedItem!==null){
        selectedItem.SetHasClass("AbilitiesShopItemSelected",false)
        selectedItem=null
    }
    if (data){
        var length=0
        Object.keys(data).forEach( function( key, index ) {
            length++
            var item=abilitiesShopELE.FindChild( "item"+index )
            var itemData = data[key];
            if(item===null){
                item = $.CreatePanel( "Panel", abilitiesShopELE, "item"+index );
                item.BLoadLayout( "file://{resources}/layout/custom_game/abilities_shop_item.xml", false, false );
            }
            item.SetItemData(itemData)
            item.index=index


        });
        ShowAbilitiesShop(length===Global.GameConfig.abilitiesShopRandomCount)
        CloseTopBtn()
    }
}

function RefreshAbility(abilityType) {
    GameEvents.SendCustomGameEventToServer("refresh_ability",
        {
            playerId:Game.GetLocalPlayerID(),
            abilityType:abilityType,
        });
}

function BuyAbility(index) {
    if(selectedItem!==null){
        isBuying=true;
        selectedItem.OnBuy(function () {
            GameEvents.SendCustomGameEventToServer("buy_ability",
                {
                    playerId:Game.GetLocalPlayerID(),
                    abilitiesHeroIndex:index,
                    abilitiesShopIndex:selectedItem.index
                });
            isBuying=false;
        })
    }
}


function OnAbilitiesShopChanged(table, key, data) {
    // $.Msg("OnAbilitiesShopChanged",data)
    if (topBtns.length===0){
        InitTopBtn()
        $("#AbilitiesShopBtn").style.position="0px 0px 0px";
        $("#AbilitiesShopBtn").style.transform="scale3d( 1,1,1)";
    }
    if ( key == Game.GetLocalPlayerID() ) {
        RefreshAbilitiesShop(data)
    }
}


    var tableData = CustomNetTables.GetTableValue("abilities_shop", Game.GetLocalPlayerID());
    if(tableData!==undefined){
        OnAbilitiesShopChanged("abilities_shop",Game.GetLocalPlayerID(), tableData);
    }



    abilitiesShopELE.OnSelectedItem = OnSelectedItem;
    // GameEvents.Subscribe( "toggle_abilities_shop", OnToggleAbilitiesShop );
    CustomNetTables.SubscribeNetTableListener("abilities_shop", OnAbilitiesShopChanged);

})(GameUI.CustomUIConfig(), this);





